package io.renren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.hutool.core.util.StrUtil;
import dm.jdbc.util.StringUtil;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.GeoDistance;
import io.renren.dao.StoreinfoDao;
import io.renren.dao.UcmlOrganizeDao;
import io.renren.dto.UcmlOrganizeDTO;
import io.renren.dto.repon.UcmlOrganizeRepon;
import io.renren.entity.StoreinfoEntity;
import io.renren.entity.UcmlOrganizeEntity;
import io.renren.service.StoreinfoService;
import io.renren.service.UcmlOrganizeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * ${comments}
 *
 * @author Sen 524227263@qq.com
 * @since 1.0.0 2024-05-28
 */
@Service
public class UcmlOrganizeServiceImpl extends CrudServiceImpl<UcmlOrganizeDao, UcmlOrganizeEntity, UcmlOrganizeDTO> implements UcmlOrganizeService {

    @Autowired
    private StoreinfoDao storeinfoDao;


    @Override
    public QueryWrapper<UcmlOrganizeEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<UcmlOrganizeEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public List<UcmlOrganizeRepon> getStoreList(String lat, String lon) {
        List<UcmlOrganizeRepon> result = new ArrayList<>();
        QueryWrapper<UcmlOrganizeEntity> wrapper = new QueryWrapper<>();
        wrapper.select("UCML_OrganizeOID,OrgName,Address,lat,lng");
        wrapper.eq("ORG_FLG", 1);
        wrapper.eq("ACTIVE_FLAG", 1);
        wrapper.apply("(lat IS NOT NULL AND lat !=''  AND lng IS NOT NULL AND lng !='' )");
        List<UcmlOrganizeEntity> list = baseDao.selectList(wrapper);
        int indexNo = 1;
        for (UcmlOrganizeEntity item : list) {
            UcmlOrganizeRepon map = new UcmlOrganizeRepon();
            map.setUcmlOrganizeOID(item.getUcmlOrganizeoid());
            map.setOrgName(item.getOrgname());
            map.setAddress(item.getAddress());
            map.setLat(item.getLat());
            map.setLng(item.getLng());
            QueryWrapper<StoreinfoEntity> wrapperStore = new QueryWrapper<>();
            wrapperStore.eq("Org_FK", item.getUcmlOrganizeoid());
            List<StoreinfoEntity> storeinfoEntities = storeinfoDao.selectList(wrapperStore);
            if (storeinfoEntities.size() > 0) {
                StoreinfoEntity storeinfoEntity = storeinfoEntities.get(0);
                map.setStorePic(storeinfoEntity.getStorepic());
            }
            map.setRowNumber(indexNo + "");
            if (StringUtil.isNotEmpty(lat) && StringUtil.isNotEmpty(lon) && StringUtil.isNotEmpty(item.getLat()) && StringUtil.isNotEmpty(item.getLng())) {
                Double distance = GeoDistance.calculateDistance(Double.valueOf(lat), Double.valueOf(lon), Double.valueOf(item.getLat()), Double.valueOf(item.getLng()));
                map.setDistance(String.format("%.2f", distance));
            } else {
                map.setDistance("0.00");
            }
            indexNo++;
            result.add(map);
        }
        result = result.stream().sorted(Comparator.comparing(UcmlOrganizeRepon::getDistance)).collect(Collectors.toList());
        return result;
    }

    @Override
    public UcmlOrganizeEntity findOneByOrganizeOID(String orgFk) {
        QueryWrapper<UcmlOrganizeEntity> wrapper = new QueryWrapper<>();
        wrapper.select("OrgName");
        wrapper.eq("UCML_OrganizeOID", orgFk);
        List<UcmlOrganizeEntity> list = baseDao.selectList(wrapper);
        return list.size() > 0 ? list.get(0) : null;
    }
}